﻿//配置对角
var _config = new Object({
    url:"AjaxPages/AjaxChatRequest.ashx",
    async:false,
    dataType:"json",
    type:"POST",
    msgID:<%=Application["maxCount"].ToString()%>,
    msgName:"Guest",
    msgIP:"<%=Request.UserHostAddress%>"
});

var ChatMsgObjArray = new Array()
ChatMsgObj = function(){
    this.msgID;
    this.msgName;
    this.msgIP;
    this.msgTime;
    this.msgText;
}

//开始运行的
$(document).ready(function(){
    GetMsgJsonInfo();
    $("#txtMsgName").select();
});

function GetMsgJsonInfo()
{
    GetMsgIDToLastJson(_config.msgID);
}

//修改名字
function ChangeMsgName(btn)
{
    var _msgName=$("#txtMsgName").val();
    if(_msgName!="" && _msgName!="Guest")
    {
        _msgName=_msgName.substring(0,8);
        _config.msgName=_msgName;
        btn.disabled=true;
        $("#msgText_textarea").attr("disabled",false);
        $("#SendMsg_ShowMsg").text("姓名修改成功");
        $("#msgText_textarea").focus();
    }else
    {
        $("#SendMsg_ShowMsg").text("姓名不能为空/Guest");
    }
}

//得到一个ID后的所有消息
function GetMsgIDToLastJson(LastID)
{
    $.ajax({
        url:_config.url,
        data:{"_type":"GetLastToEnd","LastID":LastID},
        async:_config.async,
        dataType:_config.dataType,
        type:_config.type,
        success:function(data){
            //SetSaveObjArray(data);
            BindObjJson(data);
            if(data.length!=0)
            {
                _config.msgID=data[data.length-1]["msgID"];
            }
            setTimeout(GetMsgJsonInfo,1000);
        }
    });
}

//保存实体对象
function SetSaveObjArray(data)
{
    for(var i=0;i<data.length;i++)
    {
        var _obj = new ChatMsgObj();
        _obj.msgID=data[i]["msgID"];
        _obj.msgName=data[i]["msgName"];
        _obj.msgIP=data[i]["msgIP"];
        _obj.msgTime=data[i]["msgTime"];
        _obj.msgText=data[i]["msgText"];
        ChatMsgObjArray.push(_obj);
    }
}

//绑定对角
function BindObjJson(data)
{
    for(var i=0;i<data.length;i++)
    {
        var _html = "<div class=\"ShowMsg\"><div><span class=\"ShowSpanName\">"+data[i]["msgName"]+"</span><span class=\"ShowSpanIP\">"+data[i]["msgIP"]+"</span><span class=\"ShowSpanTime\">"+data[i]["msgTime"]+"</span></div><div class=\"ShowMsgText\"><p id=\"ShowMsg_P"+data[i]["msgID"]+"\"></p></div></div>";
        $("#Main_Show_Msg").append(_html);
        $("#ShowMsg_P"+data[i]["msgID"]).text(data[i]["msgText"]);
        if($.trim(_config.msgName) != $.trim(data[i]["msgName"]))
        {
            ChangeWindowsTitle(data[i]["msgName"]);
        }
        getScrollToEnd();
    }
}

//改变窗口标题
function ChangeWindowsTitle(msgName)
{
    if(msgName!="")
    {
        document.title="简单聊天室LiuJu----你有["+msgName+"]发来的新消息";
        setTimeout("ChangeWindowsTitle('')",4000);
    }else
    {
        document.title="简单聊天室LiuJu";
    }
}

//发送对象
function SendObj()
{
    var _obj = new ChatMsgObj()
    _obj.msgName=_config.msgName;
    _obj.msgIP=_config.msgIP;
    _obj.msgTime=Date().toString();
    _obj.msgText=$("#msgText_textarea").val();
    
    if(_obj.msgText=="")
    {
        return;
    }
    
    $.post(_config.url,{"_type":"SendChatMsg","msgName":_obj.msgName,"msgIP":_obj.msgIP,"msgTime":_obj.msgTime,"msgText":_obj.msgText},function(data){
        $("#msgText_textarea").val("");
        document.getElementById("msgText_textarea").focus();
        $("#SendMsg_ShowMsg").text("信息发送成功"+data["msgTime"]);
    },_config.dataType);
}
//移动流动条
function getScrollToEnd()
{
    var MsgDiv=document.getElementById("Main_Show_Msg");
    MsgDiv.scrollTop = MsgDiv.scrollHeight - MsgDiv.clientHeight;
}
//Ctrl+Enter
function CtrlAndEnter(e)
{
    if(e.ctrlKey && e.keyCode==13)
    {
        SendObj();
    }
}
//Enter
function EnterToChangeNmae(e)
{
    if(e.keyCode==13)
    {
        $("#btn_chageName").click();
    }
}